package com.shujia.wyh.mysql_jdbc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
import java.util.UUID;

public class JDBCDemo4 {
    static Connection conn = null;
    static Statement statement = null;
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        try {
            //创建一个配置文件对象
            Properties prop = new Properties();
            //加载配置文件
            prop.load(new BufferedReader(new FileReader("src/com/shujia/wyh/mysql_jdbc/mysqlinfo.properties")));
            //根据key获取对应的value值
            String url = prop.getProperty("url");
            String user = prop.getProperty("user");
            String password = prop.getProperty("password");

            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");

            //获取连接对象
            conn = DriverManager.getConnection(url, user, password);
            //获取操作数据库对象
            statement = conn.createStatement();

            System.out.println("==================欢迎来到李佳豪婚恋介绍所=====================");
            System.out.print("欢迎登录，请输入用户名：");
            String name1 = sc.nextLine();
            System.out.print("请输入密码：");
            String pwd1 = sc.nextLine();
            //编写sql语句查询数据库中是否有该用户
            //select select username,password from login where username='xiaohu' and '123456';
            String sql = "select username,password from login where username='" + name1 + "' and password='" + pwd1 + "'";
            System.out.println(sql); //select username,password from login where username='杨志' and password='123' or '1'='1'
            //123' or '1'='1
            //执行查询sql语句
            ResultSet resultSet = statement.executeQuery(sql);
            if (!resultSet.next()) {
                System.out.println("对不起，该用户未注册，请先注册");
                System.out.println("是否要进行注册？（Y/N）:");
                String flag = sc.nextLine();
                if ("Y".equals(flag)) {
                    register();
                }else if("N".equals(flag)){
                    System.out.println("欢迎下次使用!!");
                }
            }else {
                System.out.println("登录成功！！祝您早日步入婚姻殿堂！>_^");
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放资源
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //注册功能
    public static void register() {
        //欢迎注册李佳豪婚恋介绍所
        System.out.println("欢迎注册李佳豪婚恋介绍所");
        System.out.print("请输入新用户的用户名：");
        String name = sc.nextLine();
        System.out.print("请输入新用户的密码：");
        String pwd = sc.nextLine();
        System.out.println("注册中，请稍等.....^_^");

        //java中提供了一个类UUID
        String id = UUID.randomUUID().toString();

        String sql = "insert into login value('" + id + "','" + name + "','" + pwd + "')";
        try {
            int i = statement.executeUpdate(sql);
            Thread.sleep(3000);
            if(i==1){
                System.out.println("新用户注册成功，正在跳转首页。。。");
            }else {
                System.out.println("新用户注册失败，请1小时后再进行操作..");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
